package model.load
{
	import flash.net.URLLoaderDataFormat;
	import flash.utils.Dictionary;

	public class DisplayObjectLoadMessage extends AbsLoadMessage
	{
		private static var cacheDic:Dictionary = new Dictionary();
		
		private var percentFunction:Function;
		private var completeFunction:Function;
		private var isCache:Boolean; 
		
		public function DisplayObjectLoadMessage(completeFunction:Function,percentFunction:Function=null,isCache:Boolean = false)
		{
			this.isCache = isCache;
			this.percentFunction = percentFunction;
			this.completeFunction = completeFunction;
			this._dataFormat = URLLoaderDataFormat.BINARY;
			this.timeout = 20000;
			_isDisplayObject = true;
		}
		
		override public function back(...args) : void
		{
			if (isCache)
			{
				cacheDic[this.url] = args[0];
			}
			completeFunction(args[0]);
		}
		
		override public function set percent(v:Number) : void
		{
			trace("precent",v);
			if (percentFunction != null)
			{
				percentFunction(v);
			}
		}
		
		override public function send() : void
		{
			if (isCache)
			{
				if (cacheDic[this.url] == null)
				{
					super.send();
				}
				else
				{
					back(cacheDic[this.url]);
				}
			}
			else
			{
				super.send();
			}
		}
		
		public function destroy():void
		{
			percentFunction = null;
			completeFunction = null;
		}
	}
}